package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.gps;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.*;
import com.xiaoxu.intranetweb.util.SzResult;
import org.apache.rocketmq.shaded.com.google.common.collect.Lists;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvNuo
 * @since 2025-04-02
 */
@RestController
@RequestMapping("/gps18DeviceBindTruck")
public class Gps18DeviceBindTruckController {
    @Autowired
    private Gps18DeviceBindTruckServiceImpl gps18DeviceBindTruckService;

    @Autowired
    private Gps18DeviceListServiceImpl gps18DeviceListService;

    @Autowired
    private CTrucksServiceImpl trucksService;

    @Autowired
    private CCompanysServiceImpl companyService;

    @Autowired
    private CWeightSheetsServiceImpl weightSheetsService;

    @Transactional
    @PostMapping("/bindingGps18Devic")
    public SzResult bindingGps18Devic(@RequestBody Gps18DeviceBindTruck gps18DeviceBindTruck){
        QueryWrapper<CTrucks> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCKS_TRUCKNO",gps18DeviceBindTruck.getTruckNo());
        queryWrapper.eq("TRUCKS_DELETED",0);
        CTrucks trucks = trucksService.getOne(queryWrapper);
        if (trucks == null) {
            return  SzResult.error("车辆不存在");
        }
        QueryWrapper<Gps18DeviceList> gps18DeviceListQueryWrapper = new QueryWrapper<>();
        gps18DeviceListQueryWrapper.eq(gps18DeviceBindTruck.getMacNumber() != null,"MAC_NUMBER",gps18DeviceBindTruck.getMacNumber());
        Gps18DeviceList gps18Device = gps18DeviceListService.getOne(gps18DeviceListQueryWrapper);
        if (gps18Device == null) {
           return SzResult.error("没有该定位器信息");
        }
        QueryWrapper<Gps18DeviceBindTruck> gps18DeviceBindTruckQueryWrapper = new QueryWrapper<>();
        gps18DeviceBindTruckQueryWrapper.or(i ->i.eq("TRUCK_NO",trucks.getTrucksTruckno()).or().eq("MAC_NUMBER",gps18DeviceBindTruck.getMacNumber()));
        gps18DeviceBindTruckQueryWrapper.eq("INUSE",1);
        if (gps18DeviceBindTruckService.getOne(gps18DeviceBindTruckQueryWrapper) != null) {
            return SzResult.error("该定位器或车辆已被绑定");
        }
        if (trucks.getTrucksCheckinCo() != null && !trucks.getTrucksCheckinCo().equals("")) {
            QueryWrapper<CCompanys> cCompanysQueryWrapper = new QueryWrapper<>();
            cCompanysQueryWrapper.eq("ID",trucks.getTrucksCheckinCo());
            CCompanys cCompanys = companyService.getOne(cCompanysQueryWrapper);
            gps18DeviceBindTruck.setCpyName(cCompanys.getCpyCo());
        }
        gps18DeviceBindTruck.setObjectid(gps18Device.getObjectid());
        gps18DeviceBindTruck.setInuse(1);
        gps18DeviceBindTruck.setBindTime(LocalDateTime.now());
        gps18DeviceBindTruckService.save(gps18DeviceBindTruck);
        trucks.setTrucksGpsDevId(gps18Device.getObjectid());
        trucks.setTrucksGpsDevNo(gps18DeviceBindTruck.getMacNumber());
        if (trucksService.updateById(trucks)) {
            return SzResult.success("绑定成功");
        }
        return SzResult.error("绑定失败");
    }
    @Transactional
    @PostMapping("/unbindGps18Devic")
    public SzResult unbindGps18Devic(@RequestBody Gps18DeviceBindTruck gps18DeviceBindTruck){
        QueryWrapper<CTrucks> trucksQueryWrapper = new QueryWrapper<>();
        trucksQueryWrapper.eq("TRUCKS_TRUCKNO",gps18DeviceBindTruck.getTruckNo());
        trucksQueryWrapper.eq("TRUCKS_DELETED",0);
        CTrucks trucks = trucksService.getOne(trucksQueryWrapper);
        if (trucks == null) {
            return SzResult.error("车辆不存在");
        }
        QueryWrapper<Gps18DeviceBindTruck> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TRUCK_NO",gps18DeviceBindTruck.getTruckNo());
        queryWrapper.eq("INUSE",1);
        Gps18DeviceBindTruck deviceBindTruckServiceOne = gps18DeviceBindTruckService.getOne(queryWrapper);
        if (deviceBindTruckServiceOne == null) {
            return  SzResult.error("已解绑");
        }
        ArrayList<String> reshipmentString = Lists.newArrayList("二期炉渣（倒运）", "一期炉渣（倒运）","粗灰（倒运）","细灰（倒运）");
        if (reshipmentString.contains(deviceBindTruckServiceOne.getPdtName())) {
            QueryWrapper<CWeightSheets> weightSheetsQueryWrapper = new QueryWrapper<>();
            weightSheetsQueryWrapper.eq("WEISHT_PRODUCT",deviceBindTruckServiceOne.getPdtName());
            weightSheetsQueryWrapper.orderByDesc("WEISHT_EMPTY_TM");
            List<CWeightSheets> list = weightSheetsService.list(weightSheetsQueryWrapper);
            if (list.size() == 0) {
                return  SzResult.error("没有倒运记录");
            }
            CWeightSheets weightSheets = list.get(0);
            if (weightSheets.getWeishtNetWght() == null) {
                return  SzResult.error("该车有未完成磅单");
            }
            if (weightSheets.getWeishtNetWght().equals(BigDecimal.ZERO)){
                return  SzResult.error("该车还有倒运灰未卸");
            }
        }
        deviceBindTruckServiceOne.setInuse(0);
        deviceBindTruckServiceOne.setUnbindTime(LocalDateTime.now());
        gps18DeviceBindTruckService.updateById(deviceBindTruckServiceOne);
        trucks.setTrucksGpsDevId(null);
        trucks.setTrucksGpsDevNo(0);
        if (trucksService.updateById(trucks)) {
            return SzResult.success("解绑成功");
        }
        return SzResult.error("解绑失败");
    }

    @GetMapping("/getBindingGps18Devic")
    public SzResult getBindingGps18Devic( @RequestParam(value = "page", required = false) Integer page,
                             @RequestParam(value = "pageSize", required = false) Integer pageSize,
                             @RequestParam(value = "truckNo", required = false) String truckNo){
        return gps18DeviceBindTruckService.getList(page,pageSize,1,truckNo,null,null,null);
    }

    @GetMapping("/getBindingGps18Devichistory")
    public SzResult getBindingGps18Devichistory( @RequestParam(value = "page", required = false) Integer page,
                                                 @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                                 @RequestParam(value = "truckNo", required = false) String truckNo,
                                                 @RequestParam(value = "startTime", required = false) String startTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime,
                                                 @RequestParam(value = "macNumber", required = false) Integer macNumber){
        return gps18DeviceBindTruckService.getList(page,pageSize,0,truckNo,startTime,endTime,macNumber);
    }
}
